/**
 * MapConfigBean.java : [com.shift.converter.editors.beans] Created on 2009. 5. 5.
 * 
 * Copyright (c) 2000-2008 Shift Information & Communication Co.
 * 5F, Seongsu Venture town, 231-1, Seongsu2-Ga Seongdong-Gu, Seoul, Korea 133-826
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Shift Information & Communication Co. ("Confidential Information").
 * You shall not disclose such Confidential Information and shall use 
 * it only in accordance with the terms of the license agreement you 
 * entered into with Shift Information & Communication.
 */
package com.shift.converter.beans;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.ListenerList;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.swt.widgets.Display;

public class MapConfigBean {

	public static final String DATASOURCE_CHANGED = "MAPCONFIG_DATASOURCE_CHANGED";

	public static final String DATASOURCE_ADD = "MAPCONFIG_DATASOURCE_ADD";

	public static final String DATASOURCE_REMOVE = "MAPCONFIG_DATASOURCE_REMOVE";
	
	public static final String DATASET_CHANGED = "MAPCONFIG_DATASET_CHANGED";
	
	public static final String DATASET_ADD = "MAPCONFIG_DATASET_ADD";

	public static final String DATASET_REMOVE = "MAPCONFIG_DATASET_REMOVE";
	
	private transient ListenerList propertyChangeListeners = new ListenerList();
	
	List dataSourceList = new ArrayList();
	List dataSetList = new ArrayList();
	
	public MapConfigBean() {
	}
	
	public void setDataSourceList(List dataSourceList) {
		List oldValue = this.dataSourceList;
		this.dataSourceList = dataSourceList;
		firePropertyChange(DATASOURCE_CHANGED, oldValue, this);
	}
	
	public void addDataSource(DataSourceBean source) {
		dataSourceList.add(source);
		firePropertyChange(DATASOURCE_ADD, null, source);
	}

	public void setDataSetList(List dataSetList) {
		List oldValue = this.dataSetList;
		this.dataSetList = dataSetList;
		firePropertyChange(DATASET_CHANGED, oldValue, this);
	}
	
	public DataSourceBean[] getDataSourceList() {
		return (DataSourceBean[]) dataSourceList.toArray(new DataSourceBean[0]);
	}
	
	public DataSourceBean getDataSource(int	index) {
		return (DataSourceBean)dataSourceList.get(index);
	}
	
	public boolean hasDataSourceBean() {
		return dataSourceList.size() > 0;
	}
	
	public void removeDataSource(DataSourceBean dataSource) {
		dataSourceList.remove(dataSource);
		firePropertyChange(DATASOURCE_REMOVE, dataSource, null);
	}
	
	public void addDataSet(DataSetBean dataSet) {
		dataSetList.add(dataSet);
		firePropertyChange(DATASET_ADD, null, dataSet);
	}
	
	public void removeDataSet(DataSetBean dataSet) {
		dataSetList.remove(dataSet);
		firePropertyChange(DATASET_REMOVE, dataSet, null);
	}
	
	public DataSetBean[] getDataSetList() {
		return (DataSetBean[]) dataSetList.toArray(new DataSetBean[0]);
	}
	
	public DataSetBean getDataSet(int index) {
		return (DataSetBean)dataSetList.get(index);
	}
	
	
	public void addPropertyChangeListener(IPropertyChangeListener listener) {
		getPropertyChangeListeners().add(listener);
	}
	
	public boolean hasDataSetBean() {
		return dataSetList.size() > 0;
	}

	/**
	 * Removes a property change listener.
	 * 
	 * @param listener
	 */
	public void removePropertyChangeListener(IPropertyChangeListener listener) {
		getPropertyChangeListeners().remove(listener);
	}

	/**
	 * Fires a property change event object to all registered listeners. These
	 * events are sent on the UI thread.
	 * 
	 * @param changeId
	 *            - Type of change: LISTADD or LISTREMOVE
	 * @param oldValue
	 *            - null for LISTADD, Removed Location for LISTREMOVE
	 * @param newValue
	 *            - null for LISTREMOVE, Added Location for LISTADD
	 */
	void firePropertyChange(String changeId, Object oldValue, Object newValue) {
		final PropertyChangeEvent event = new PropertyChangeEvent(this,
				changeId, oldValue, newValue);

		Display.getDefault().syncExec(new Runnable() {
			public void run() {
				Object[] listeners = getPropertyChangeListeners()
						.getListeners();
				for (int i = 0; i < listeners.length; i++) {
					((IPropertyChangeListener) listeners[i])
							.propertyChange(event);
				}
			}
		});
	}

	/**
	 * @return Returns the propertyChangeListeners.
	 */
	private ListenerList getPropertyChangeListeners() {
		if (propertyChangeListeners == null)
			propertyChangeListeners = new ListenerList();
		return propertyChangeListeners;
	}
}
